package com.cxg.fairy.ec.main.personal.history;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;
import com.cxg.fairy.ui.recycler.SwipeItemLayout;
import com.cxg.fairy.util.log.LatteLogger;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/3/8.
 */

public class HistoryDelegate extends LatteDelegate implements ISuccess {

    @BindView(R2.id.rv_favor_list)
    RecyclerView mRecyclerView = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_history_list;
    }

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    @OnClick(R2.id.itv_history_clear)
    void onClickClearHistory() {
        new AlertDialog.Builder(getContext())
                .setTitle(getResources().getString(R.string.history_clear_title))
                .setMessage(getResources().getString(R.string.history_clear_content))
                .setNegativeButton(getResources().getString(R.string.app_button_cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .setPositiveButton(getResources().getString(R.string.app_button_ok), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        RestClient.builder()
                                .url("emptyGoodsBrowse")
                                .params("uuid",AccountManager.getSignUUID())
                                .loader(getContext())
                                .success(new ISuccess() {
                                    @Override
                                    public void onSuccess(String response) {
                                        HistoryAdapter adapter = (HistoryAdapter) mRecyclerView.getAdapter();
                                        adapter.clearAll();
                                    }
                                })
                                .build()
                                .post();

                    }
                })
                .create().show();
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        RestClient.builder()
                .url("getgoodsBrowses")
                .params("uuid", AccountManager.getSignUUID())
                .loader(getContext())
                .success(this)
                .build()
                .post();
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onSuccess(String response) {
        LatteLogger.e(this.getSupportDelegate().toString(), AccountManager.getSignUUID());
        LatteLogger.e(this.getSupportDelegate().toString(), response);

        final GridLayoutManager manager = new GridLayoutManager(getContext(), 30);
        mRecyclerView.setLayoutManager(manager);

        final List<MultipleItemEntity> data = new HistoryDataConverter().setJsonData(response).convert();
        final HistoryAdapter favorAdapter = new HistoryAdapter(data);

        mRecyclerView.setAdapter(favorAdapter);
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this.getContext(),DividerItemDecoration.VERTICAL);

        dividerItemDecoration.setDrawable(getContext().getResources().getDrawable(R.drawable.my_decoration_single));
        mRecyclerView.addItemDecoration(dividerItemDecoration);

        mRecyclerView.addOnItemTouchListener(new SwipeItemLayout.OnSwipeItemTouchListener(getContext()));

    }

}
